//
//  MDHPageControlJackYoungCustom.swift
//  JSTech
//
//  Created by 华&梅 on 2019/8/5.
//  Copyright © 2020 华&梅. All rights reserved.
//

import UIKit

class MDHPageControlJackYoungCustom: CHIBasePageControl {

    @IBInspectable open var inactiveElementWidth: CGFloat = 20 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable open var inactiveElementHeight: CGFloat = 6 {
        didSet {
            setNeedsLayout()
        }
    }
    
    @IBInspectable open var activeElementWidth: CGFloat = 20 {
        didSet {
            setNeedsLayout()
        }
    }

    @IBInspectable open var activeElementHeight: CGFloat = 6 {
        didSet {
            setNeedsLayout()
        }
    }

    fileprivate var inactive = [CHILayer]()
    fileprivate var active = CHILayer()

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
    }

    override func updateNumberOfPages(_ count: Int) {//根据数量创建inactive数组
        inactive.forEach { $0.removeFromSuperlayer() }
        inactive = [CHILayer]()
        inactive = (0..<count).map {_ in
            let layer = CHILayer()
            self.layer.addSublayer(layer)
            return layer
        }

        self.layer.addSublayer(active)

        setNeedsLayout()
        self.invalidateIntrinsicContentSize()
    }

    override open func layoutSubviews() {//初始化inactive布局
        super.layoutSubviews()
        
        if inactive.count <= 0 {
            return
        }
        
        let floatCount = CGFloat(inactive.count)
        let tempX = self.inactiveElementWidth * (floatCount - 1) + self.padding * (floatCount - 1) + self.activeElementWidth
        let x = (self.bounds.size.width - tempX) * 0.5
        let y = (self.bounds.size.height - self.inactiveElementHeight) * 0.5
        var frame = CGRect(x: x, y: y, width: self.inactiveElementWidth, height: self.inactiveElementHeight)

        active.cornerRadius = self.radius
        active.backgroundColor = (self.currentPageTintColor ?? self.tintColor)?.cgColor
        active.frame = frame

        inactive.enumerated().forEach() { index, layer in
            layer.backgroundColor = self.tintColor(position: index).withAlphaComponent(self.inactiveTransparency).cgColor
            if self.borderWidth > 0 {
                layer.borderWidth = self.borderWidth
                layer.borderColor = self.tintColor(position: index).cgColor
            }
            layer.cornerRadius = self.radius
            layer.frame = frame
            frame.origin.x += self.inactiveElementWidth + self.padding
        }
        update(for: progress)
    }

    override func update(for progress: Double) {//更新位置
        guard let min = inactive.first?.frame,
              progress >= 0,
              numberOfPages > 1 else {
                return
        }
        //&& progress <= Double(numberOfPages - 1) let max = inactive.last?.frame,

        
        var recentX : CGFloat = 0
        for i in 0..<inactive.count {
            let item = inactive[i]
            if i == Int(ceil(progress)) {
                item.frame = CGRect(x: min.origin.x + recentX, y: min.origin.y, width: self.activeElementWidth, height: self.activeElementHeight)
                active.frame = item.frame
                recentX = recentX + self.activeElementWidth + padding
            } else {
                item.frame = CGRect(x: min.origin.x + recentX, y: min.origin.y, width: self.inactiveElementWidth, height: self.inactiveElementHeight)
                recentX = recentX + self.inactiveElementWidth + padding
            }
        }
    }

    override open var intrinsicContentSize: CGSize {
        return sizeThatFits(CGSize.zero)
    }

    override open func sizeThatFits(_ size: CGSize) -> CGSize {//整个大小
        return CGSize(width: CGFloat(inactive.count - 1) * self.inactiveElementWidth + self.activeElementWidth + CGFloat(inactive.count - 1) * self.padding,
                      height: self.inactiveElementHeight)
    }


}
